package com.googlecode.xcache.transcode.impl;

import com.googlecode.xcache.impl.memcachedclient.ValueCaster;
import com.googlecode.xcache.transcode.TransCoder;

/**
 * A base {@link TransCoder} implementation that supports some common transcoding,
 * including casting Strings to basic types (int, double, etc.).
 * 
 * @author tielei
 *
 */
public abstract class BaseTransCoder implements TransCoder {
	private ValueCaster valueCaster = new ValueCaster();

	@Override
	public Object encode(Object value) {
		return value;
	}

	@Override
	public <T> T decode(Class<T> expectedValueClass, Object value) {
		Object decodedValue = customDecode(expectedValueClass, value);
		return valueCaster.cast(expectedValueClass, decodedValue);
	}

	/**
	 * Subclasses can override this method to perform custom decoding.
	 * @param <T>
	 * @param expectedValueClass expected type of  value.
	 * @param value encoded value.
	 * @return original value.
	 */
	public <T> Object customDecode(Class<T> expectedValueClass, Object value) {
		return value;
	}
}
